package com.woniu.controller;

import com.woniu.entity.Result;
import com.woniu.entity.vo.UserCountDayVO;
import com.woniu.service.IUserService;
import com.woniu.util.DateUtils;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Title
 * @Author 武术有栖
 * @Description:
 */
@RestController
@RequestMapping("/count")
public class CountController extends BaseController {
    @Autowired
    private IUserService userService;

    @GetMapping("/user/{type}")
    public Result countUsersByDays(@PathVariable String type) {
        DateUtils dateUtils = new DateUtils();
        List<UserCountDayVO> userCountDayVOList = new ArrayList<>();
        List<String> day = new ArrayList<>();
        if (Objects.equals(type, "1")) {
            userCountDayVOList = userService.countUsersByToday();
            Date date = new Date();
            SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
            day.add(formatter.format(date));
        } else if (Objects.equals(type, "2")) {
            userCountDayVOList = userService.countUsersByYesterday();
            day = dateUtils.getYesterday();
        } else if (Objects.equals(type, "3")) {
            userCountDayVOList = userService.countUsersByWeek();
            day = dateUtils.getCurrentWeekDays();
        } else if (Objects.equals(type, "4")) {
            day = dateUtils.getCurrentMonthDays();
            userCountDayVOList = userService.countUsersByMonth();
        }
        // 获取横轴数据
        List<String> xList = userCountDayVOList.stream().map(UserCountDayVO::getDateStr).collect(Collectors.toList());
        // 获取竖轴数据
        List<Integer> yList = userCountDayVOList.stream().map(UserCountDayVO::getCount).collect(Collectors.toList());

        for (String s : day) {
            if (!xList.contains(s)) {
                xList.add(s);
                yList.add(0);
            }
        }
        Map<String, Object> userDaysCount = new HashMap<>();
        userDaysCount.put("xList", xList);
        userDaysCount.put("yList", yList);
        System.out.println(userDaysCount);
        return toDataResult(userDaysCount);
    }
}
